const cottonFields = [
  { label: '播种-出苗天数', type: 'number' },
  { label: '出苗-现蕾天数', type: 'number' },
  { label: '现蕾-开花天数', type: 'number' },
  { label: '开花-吐絮天数', type: 'number' },
  { label: '整个生育期天数', type: 'number' },
  { label: '整体株型', type: 'text' },
  { label: '果枝长短', type: 'text' },
  { label: '果枝夹角', type: 'text' },
  { label: '茎秆粗细', type: 'text' },
  { label: '红茎比例', type: 'text' },
  { label: '叶片是否含绒毛', type: 'picker', options: ['是', '否'] },
  { label: '叶片大小', type: 'text' },
  { label: '叶片厚度', type: 'text' },
  { label: '单位面积有效株数', type: 'number' },
  { label: '单位面积有效铃数', type: 'number' },
  { label: '单位面积籽棉产量', type: 'number' },
  { label: '单位面积皮棉产量', type: 'number' },
  { label: '纤维长度', type: 'number' },
  { label: '断裂比强度', type: 'number' },
  { label: '马克隆值', type: 'number' },
  { label: '整齐度', type: 'number' },
  { label: '断裂伸长率', type: 'number' },
  { label: '苗期病害调查', type: 'text' },
  { label: '立枯病', type: 'text' },
  { label: '生长期病害调查', type: 'text' },
  { label: '枯萎病', type: 'text' },
  { label: '铃期病害调查', type: 'text' },
  { label: '黄萎病', type: 'text' },
  { label: '红腐病', type: 'text' },
  { label: '虫害调查', type: 'text' },
  { label: '棉蚜', type: 'text' },
  { label: '蓟马', type: 'text' },
  { label: '红蜘蛛', type: 'text' },
  { label: '棉铃虫', type: 'text' }
];

Page({
  data: {
    group: 1,
    row: 0,
    col: 0,
    formData: {},
    cottonFields,
    images: [],
    extraInfo: ''
  },
  onLoad(options) {
    const group = Number(options.group);
    const row = Number(options.row);
    const col = Number(options.col);
    let allMatrix = wx.getStorageSync('allMatrix') || [];
    let formData = {};
    let images = [];
    let extraInfo = '';
    if (
      allMatrix[group - 1] &&
      allMatrix[group - 1][row] &&
      allMatrix[group - 1][row][col]
    ) {
      if (allMatrix[group - 1][row][col].value) {
        formData = JSON.parse(JSON.stringify(allMatrix[group - 1][row][col].value));
      }
      if (allMatrix[group - 1][row][col].images) {
        images = allMatrix[group - 1][row][col].images;
      }
      if (allMatrix[group - 1][row][col].extraInfo) {
        extraInfo = allMatrix[group - 1][row][col].extraInfo;
      }
    }
    this.setData({ group, row, col, formData, images, extraInfo });
  },
  onInput(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({ [`formData.${field}`]: value });
  },
  onPickerChange(e) {
    const field = e.currentTarget.dataset.field;
    const options = this.data.cottonFields.find(f => f.label === field).options;
    const value = options[e.detail.value];
    this.setData({ [`formData.${field}`]: value });
  },
  // 选择图片
  onChooseImage() {
    const that = this;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        that.setData({
          images: that.data.images.concat(res.tempFilePaths)
        });
      }
    });
  },
  // 补充信息输入
  onExtraInfoInput(e) {
    this.setData({ extraInfo: e.detail.value });
  },
  onSubmit() {
    const { group, row, col, formData, images, extraInfo } = this.data;
    let allMatrix = wx.getStorageSync('allMatrix') || [];
    if (!allMatrix[group - 1]) allMatrix[group - 1] = [];
    if (!allMatrix[group - 1][row]) allMatrix[group - 1][row] = [];
    if (!allMatrix[group - 1][row][col]) allMatrix[group - 1][row][col] = { row, col, value: {} };
    allMatrix[group - 1][row][col].value = JSON.parse(JSON.stringify(formData));
    allMatrix[group - 1][row][col].images = images;
    allMatrix[group - 1][row][col].extraInfo = extraInfo;
    wx.setStorageSync('allMatrix', allMatrix);
    wx.showToast({ title: '已保存', icon: 'success' });
    wx.navigateBack();
  }
}); 